
const global = getApp().globalData;
const Api = global.Api;
const wxApi = global.wxApi;

Page({
  data: {
    topics: [],
    loadmoreType: 0,
    topicTotalNum: 0,
    lesson: true,
    infoViewShow: false
  },
  onLoad(options) {
    wxApi.setNavTitle('知识点列表');
    this.init(options);
  },
  init(options) {
    this.pageNo = 1;
    this.topicAPI = global.topicApi;
    this.logger = global.logger;
    let searchParam = options.searchParam;
    let collectParam = options.collectParam;
    if (searchParam) {
      this.mode = 'search'
      this.setData({
        lesson: false
      })
      searchParam = wxApi.decodeURI(searchParam);
      this.searchModeInit(searchParam);
    } else if (collectParam) {
      this.mode = 'collect'
      this.setData({
        lesson: false
      })
      this.collectModeInit();
    }
    this.refresh();
  },
  /**
   * 一般搜索知识点模式初始化
   */
  searchModeInit(params) {
    this.query = params.query;
    wxApi.setNavTitle(params.title);
  },
  /**
   * 单元查询知识点模式初始化
   */
  unitModeInit(params) {
    this.bookId = params.bookId;
    this.unitId = params.unitId;
    wxApi.setNavTitle(params.unitName);
  },
  /**
   * 收藏知识点模式，初始化
   */
  collectModeInit() {

  },
  onTopicDetail(e) {
    let dataset = e.currentTarget.dataset
    let topic = e.detail;
    let params = {
      topic: wxApi.encodeURI(topic)
    }
    let path = '../topic-detail/topic-detail';
    wxApi.navigateTo(path, params);
  },
  refresh() {
    this.pageNo = 1;
    this.loadTopics(this.pageNo, topics => {
      this.setTopics( topics );
    });
  },
  onReachBottom() {
    this.loadMore();
  },
  loadMore(fn) {
    // 显示加载更多
    this.setLoadmoreType( 1 );
    this.pageNo ++;
    let totalTopics = this.data.topics;
    this.loadTopics( this.pageNo, topics => {
      if ( !topics.length ) {
        // 显示没有更多
        this.pageNo --;
        this.setLoadmoreType( 2 );
        fn && fn(false) // 无更多
        return;
      }
      this.setLoadmoreType( 0 )
      totalTopics = totalTopics.concat( topics );
      this.setTopics( totalTopics );
      fn && fn(true) // 有更多
    } )

  },
  setTopics( topics ) {
    console.log(topics);
    let infoViewShow = false;
    infoViewShow = !topics.length;
    this.setData({
      topics: topics,
      infoViewShow: infoViewShow
    })
  },
  setLoadmoreType( val ) {
    this.setData({
      loadmoreType: val
    })
  },
  loadTopics(pageNo, callback) {
    if (this.mode === 'search') {
      let params = {
        pageNo: pageNo,
        ...this.query
      }
      console.log(params);
      this.topicAPI.searchTopics(params, data => {
        this.setData({
          topicTotalNum: data.total
        })
        callback && callback( data.records );
      })
    }
    if (this.mode === 'collect') {
      let params = {
        pageNo: pageNo
      }
      this.topicAPI.getCollectedTopics(params, data => {
        this.setData({
          topicTotalNum: data.total
        })
        callback && callback(data.records);
      })
    } 
  }
}) 